package com.unipay.cmn.controller;

import com.unipay.cmn.service.DictService;
import com.unipay.common.result.Result;
import com.unipay.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-04-01 21:24:21
 * @Desc: TODO
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/unipay/hospSet/cmn")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    //数据字典，“导出”功能
    @ApiOperation(value = "上传")
    @PostMapping("/importData")
    public void importData(MultipartFile file) {
        dictService.importData(file);
    }

    //数据字典，“导出”功能
    @ApiOperation(value = "导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    //根据数据id查询子数据列表
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping(value = "/findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    //TODO 根据上级编码dictcode 与 值value，获取数据字典名称
    @ApiOperation(value = "获取数据字典名称 1")
    @GetMapping("/getName/{dictcode}/{value}")
    public String getName(
            @PathVariable("dictcode") String dictcode,
            @PathVariable("value") String value
    ) {
        String dictName = dictService.getNameByParentDictCodeAndValue(dictcode, value);
        return dictName;
    }

    //TODO 根据value查询
    @ApiOperation(value = "获取数据字典名称 2")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable("value") String value){
        String dictName = dictService.getNameByParentDictCodeAndValue("", value);
        return dictName;
    }

    //TODO 数据字典“级联显示”接口
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> childList = dictService.findByDictCode(dictCode);
        return Result.ok(childList);
    }
}
